<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  xmlns:skos='http://www.w3.org/2004/02/skos/core#'>

  <db:article>
    <db:info>
      <db:title>Extract ATC File</db:title>

      <db:copyright>
        <db:year>2011</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.1.1</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-11-03</db:date>
          <db:authorinitials>BD</db:authorinitials>
          <db:revremark>Updated for PBS XML Schema v2.6.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-02-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML Schema v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-09-25</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the ATC extract file (<db:filename>ATC yyyymmdd.txt</db:filename>) from a PBS XML document.</db:para>
    <db:para>The ATC extract file provides information on ATC codes and their meanings.  The ATC codes are used to provide the sort order for the drugs in the Schedule.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The default field delimeter in this file is the exclamation mark (!).</db:para>
    <db:table>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>ATC Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>20</db:entry>
            <db:entry>
              <db:para>Code as published in the current ATC Index issued by the WHO collaborating Centre for Drug Statistics Methodology, in a five level hierarchy in format annaann, as follows:</db:para>
              <db:simplelist>
                <db:member>First level - a - anatomical main group</db:member>
                <db:member>Second level - nn - therapeutic main group</db:member>
                <db:member>Third level - a - therapeutic subgroup</db:member>
                <db:member>Fourth level - a - chemical/therapeutic subgroup</db:member>
                <db:member>Fifth level - nn - subgroup for chemical substance</db:member>
              </db:simplelist>
            </db:entry>
          </db:row>
          <db:row>
            <db:entry>ATC Meaning</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>230</db:entry>
            <db:entry>Meaning for ATC code as published in the current ATC Index issued by the WHO Collaborating Centre for Drug Statistics Methodology.</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method='text'/>

  <xsl:variable name='delimiter' select='"!"'/>

  <xsl:key name='rdf'
    match='skos:Concept'
    use='@rdf:about'/>

  <db:template name='root'>
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML Schema.</db:para>
  </db:template>
  <xsl:template match='/'>
    <xsl:variable name='major' select='substring-before(/*/@version, ".")'/>

    <xsl:choose>
      <xsl:when test='$major >= 2'>
        <xsl:apply-templates select='//rdf:RDF/skos:Concept[@rdf:about = "http://www.whocc.no/ATC"]'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate='yes'>This stylesheet is designed to operate upon a Schedule XML document that conforms to version 2.0 (or later) of the PBS XML Schema.  See http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of this XSL stylesheet that operates on documents that conform to an earlier version of the schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='skos:Concept[@rdf:about = "http://www.whocc.no/ATC"]'>
    <xsl:apply-templates select='skos:narrower'>
      <xsl:sort select='@rdf:resource'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='skos:narrower'>
    <xsl:apply-templates
      select='key("rdf", @rdf:resource)'/>
  </xsl:template>
  <xsl:template match='skos:Concept[@rdf:about != "http://www.whocc.no/ATC"]'>
    <xsl:apply-templates select='skos:prefLabel'
      mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <xsl:apply-templates select='skos:definition'
      mode='textonly'/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates select='skos:narrower'>
      <xsl:sort select='@rdf:resource'/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="text()" mode="textonly">
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>

  <xsl:template match='text()'/>
</xsl:stylesheet>
